part of _;

extension on ProductListPage {
  void controller_initData(bool isAdd) {
    page = isAdd ? page + 1 : 1;
    yPost<ProductListModel>(IFinal.Jkids.getProductList, {
      "page": page,
      "content": searchContent,
      "search": searchItems.isEmpty ? null : searchItems,
    }, (isSuccess, results, code, message) {
      page = isSuccess ? (isAdd ? (results!.first.productItems.isEmpty ? page - 1 : page) : page) : (isAdd ? page - 1 : page);
      if (isSuccess) {
        var result = results!.first;
        if (searchItems.isEmpty) searchItems = result.searchItems;
        if (!isAdd)
          productItems = result.productItems;
        else
          productItems.addAll(result.productItems);
        setState(() {});
      } else
        yShowBuilder("数据加载失败，是否重试？\n$message", () => controller_initData(isAdd), null, true);
      myRefresherController?.loadFinish(showNoMoreData: results != null && results.isEmpty);
    }, onModel: (map) => ProductListModel.fromJson(map), showProgress: false);
  }

  void controller_closeSearchPopup(SearchMainModel model) {
    searchItems.forEach((element) {
      yDismissDialog(element.dialogContext);
      element.dialogContext = null;
      controller_anim?.reverse();
    });
    setState(() {});
  }

  void controller_showSearchPopup(SearchMainModel item, ScrollController scrollController) {
    var isShowing = item.dialogContext != null;
    controller_closeSearchPopup(item);
    if (isShowing) return;
    if (!item.controller_searchBar_inner.hasListeners) item.controller_searchBar_inner.addListener(() => scrollController.jumpTo(item.controller_searchBar_inner.offset));
    showDialog(
      context: yPage.context,
      barrierColor: Colors.transparent,
      builder: (dialogContext) {
        item.dialogContext = dialogContext;
        controller_anim!.forward();
        yRun(() => item.controller_searchBar_inner.jumpTo(controller_searchBar.offset));
        return StatefulBuilder(
          builder: (statefulContext, setDialogState) {
            return Scaffold(
              backgroundColor: Colors.transparent,
              body: GestureDetector(
                onTap: () => controller_closeSearchPopup(item),
                behavior: HitTestBehavior.translucent,
                child: Container(
                  width: yScreenSize().width,
                  height: yScreenSize().height,
                  padding: EdgeInsets.only(top: yNavBarHeight()),
                  child: Container(
                    color: Colors.black.withAlpha(100),
                    child: Stack(
                      children: [
                        Positioned(
                          top: searchBarHeight,
                          child: GestureDetector(
                            onTap: () {},
                            behavior: HitTestBehavior.translucent,
                            child: SlideTransition(
                              position: animation!,
                              child: Container(
                                width: yScreenSize().width,
                                constraints: BoxConstraints(maxHeight: yScreenSize().height / 2),
                                decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.only(bottomLeft: Radius.circular(10), bottomRight: Radius.circular(10))),
                                child: Container(
                                  decoration: BoxDecoration(color: Colors.grey.shade100, borderRadius: BorderRadius.circular(10)),
                                  child: item.isSingle ? view_single(setDialogState, item) : view_mutiplie(setDialogState, item),
                                ),
                              ),
                            ),
                          ),
                        ),
                        view_search(item.controller_searchBar_inner),
                      ],
                    ),
                  ),
                ),
              ),
            );
          },
        );
      },
    );
  }

  void controller_refreshSearchBar() {
    searchItems.forEach((element) {
      String value = "";
      element.items.forEach((element) {
        if (element.isSelect) value += "${element.title}/";
      });
      element.value = value.isEmpty ? "" : value.substring(0, value.length - 1);
    });
  }
}
